package cn.iocoder.yudao.module.member.controller.admin.major;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.major.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.major.MajorDO;
import cn.iocoder.yudao.module.member.service.major.MajorService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 专业")
@RestController
@RequestMapping("/member/major")
@Validated
public class MajorController {

    @Resource
    private MajorService majorService;

    @PostMapping("/create")
    @Operation(summary = "创建专业")
    public CommonResult<Long> createMajor(@Valid @RequestBody MajorSaveReqVO createReqVO) {
        return success(majorService.createMajor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业")
    public CommonResult<Boolean> updateMajor(@Valid @RequestBody MajorSaveReqVO updateReqVO) {
        majorService.updateMajor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteMajor(@RequestParam("id") Long id) {
        majorService.deleteMajor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MajorRespVO> getMajor(@RequestParam("id") Long id) {
        MajorDO major = majorService.getMajor(id);
        return success(BeanUtils.toBean(major, MajorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专业分页")
    public CommonResult<PageResult<MajorRespVO>> getMajorPage(@Valid MajorPageReqVO pageReqVO) {
        PageResult<MajorDO> pageResult = majorService.getMajorPage(pageReqVO);
        PageResult<MajorRespVO> bean = BeanUtils.toBean(pageResult, MajorRespVO.class);
        for (MajorRespVO respVO:bean.getList()){
            if (respVO.getPid() ==0L ){
                respVO.setParentName("顶级专业");
            } else {
                MajorDO major = majorService.getMajor(respVO.getPid());
                if (Objects.nonNull(major)) {
                    respVO.setParentName(major.getName());
                }
            }
        }
        return success(bean);
    }
    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取专业精简信息列表", description = "只包含被开启的专业，主要用于前端的下拉选项")
    public CommonResult<List<MajorRespVO>> getSimpleList() {
        List<MajorDO> list = majorService.getSimpleList();
        List<MajorRespVO> bean = BeanUtils.toBean(list, MajorRespVO.class);
        bean.stream().forEach(v1->v1.setParentId(v1.getPid()));
        return success(bean);
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出专业 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMajorExcel(@Valid MajorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MajorDO> list = majorService.getMajorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专业.xls", "数据", MajorRespVO.class,
                        BeanUtils.toBean(list, MajorRespVO.class));
    }

    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public void importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<MajorRespVO> list = ExcelUtils.read(file, MajorRespVO.class);
        majorService.importExcel(list);
    }

}